//
//  FPCustomPopupView.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/23.
//

#import "FPCustomPopupView.h"

@interface FPCustomPopupView () <UIGestureRecognizerDelegate>


@property (nonatomic, strong) UIView *backgroundView;  // 半透明背景

@end

@implementation FPCustomPopupView

- (instancetype)initWithHeight:(CGFloat)height {
    self = [super init];
    if (self) {
        _popupHeight = height;  // 设置弹窗高度
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    // 设置整个弹窗的背景为透明色
    self.backgroundColor = [UIColor clearColor];
    
    // 半透明背景视图
    self.backgroundView = [[UIView alloc] init];
    self.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self addSubview:self.backgroundView];
    
    // 自定义内容视图
    self.contentView = [[UIView alloc] init];
    self.contentView.backgroundColor = [UIColor whiteColor];
//    self.contentView.layer.cornerRadius = 20.f;
//    self.contentView.layer.masksToBounds = YES;
    [self addSubview:self.contentView];
    
    
    
    // 添加点击背景关闭手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [self.backgroundView addGestureRecognizer:tapGesture];
    tapGesture.delegate = self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 设置背景视图全屏
    self.backgroundView.frame = self.bounds;
    
    // 设置内容视图的大小和位置
//    CGFloat screenWidth = self.bounds.size.width;
    CGFloat screenHeight = self.bounds.size.height;
    self.contentView.frame = CGRectMake(0, screenHeight - self.popupHeight, SCREEN_WIDTH, self.popupHeight);
    
    [UIView setCornerForView:self.contentView withRadius:20.f bounds:CGRectMake(0.f, 0.f, SCREEN_WIDTH, self.popupHeight) corners:(UIRectCornerTopLeft | UIRectCornerTopRight)];
}

// 显示弹窗
- (void)showInView:(UIView *)view {
    self.frame = view.bounds;
    [view addSubview:self];
    
    // 设置初始位置（从屏幕底部弹出）
    CGRect finalFrame = self.contentView.frame;
    self.contentView.frame = CGRectOffset(finalFrame, 0, finalFrame.size.height);
    
    // 动画显示
    [UIView animateWithDuration:0.3 animations:^{
        
        self.contentView.frame = finalFrame;
    }];
}

// 关闭弹窗
- (void)dismiss {
    CGRect finalFrame = self.contentView.frame;
    finalFrame = CGRectOffset(finalFrame, 0, finalFrame.size.height);
    
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.frame = finalFrame;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)updateHeight:(CGFloat)newHeight {
    self.popupHeight = newHeight;
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect newFrame = self.contentView.frame;
        newFrame.size.height = newHeight;
        newFrame.origin.y = self.bounds.size.height - newHeight;
        self.contentView.frame = newFrame;
    }];
}

//- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
//
//    if ([touch.view isEqual:self]) {
//        
////        self.showTabBarBlock();
//        return YES;
//    }
//    
//    return NO;
//}



@end
